package exercise10;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Exercise16_08 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        //总布局
        BorderPane pane=new BorderPane();


        //其它组件
        Label result_label=new Label();
        result_label.setText("这两个圆是否相交呢？好像不相交啊");

        Circle circle1=new Circle();
        Circle circle2=new Circle();

        circle1.setCenterX(150);
        circle1.setCenterY(100);
        circle1.setRadius(40);
        circle2.setCenterX(450);
        circle2.setCenterY(100);
        circle2.setRadius(40);
        circle1.setFill(new Color(1,1,1,0));
        circle1.setStroke(Color.BLACK);
        circle2.setFill(new Color(1,1,1,0));
        circle2.setStroke(Color.BLACK);



        //其它布局
        HBox select_hbox=new HBox();

        Pane circle_pane=new Pane();
        circle_pane.getChildren().addAll(circle1,circle2);

        BorderPane pane1=new BorderPane();
        pane1.setTop(new Label("请输入第一个圆的信息"));
        GridPane pane1_select=new GridPane();
        pane1_select.add(new Label("横坐标："),0,0);
        pane1_select.add(new Label("纵坐标"),0,1);
        pane1_select.add(new Label("半径"),0,2);
        TextField x1_text=new TextField("150");
        TextField y1_text=new TextField("100");
        TextField r1_text=new TextField("40");
        pane1_select.add(x1_text,1,0);
        pane1_select.add(y1_text,1,1);
        pane1_select.add(r1_text,1,2);
        pane1.setCenter(pane1_select);

        BorderPane pane2=new BorderPane();
        pane2.setTop(new Label("请输入第二个圆的信息"));
        GridPane pane2_select=new GridPane();
        pane2_select.add(new Label("横坐标："),0,0);
        pane2_select.add(new Label("纵坐标"),0,1);
        pane2_select.add(new Label("半径"),0,2);
        TextField x2_text=new TextField("450");
        TextField y2_text=new TextField("100");
        TextField r2_text=new TextField("40");
        pane2_select.add(x2_text,1,0);
        pane2_select.add(y2_text,1,1);
        pane2_select.add(r2_text,1,2);
        pane2.setCenter(pane2_select);

        Button draw=new Button("绘制");
        draw.setOnAction(event -> {
            circle1.setCenterX(Double.parseDouble(x1_text.getText()));
            circle1.setCenterY(Double.parseDouble(y1_text.getText()));
            circle1.setRadius(Double.parseDouble(r1_text.getText()));
            circle2.setCenterX(Double.parseDouble(x2_text.getText()));
            circle2.setCenterY(Double.parseDouble(y2_text.getText()));
            circle2.setRadius(Double.parseDouble(r2_text.getText()));
            double d=Math.pow((Math.pow(circle1.getCenterX()-circle2.getCenterX(),2))+
                    (Math.pow(circle1.getCenterY()-circle2.getCenterY(),2)),0.5);
            if (d<=(circle1.getRadius()+circle2.getRadius())){
                result_label.setText("这两个圆相交么？恭喜你！相交了");
            }
            else {
                result_label.setText("这两个圆是否相交呢？好像不相交啊");
            }
        });

        circle1.setOnMouseDragged(event -> {
            if (circle1.contains(event.getX(),event.getY())){
                circle1.setCenterX(event.getX());
                circle1.setCenterY(event.getY());
                x1_text.setText(event.getX()+"");
                y1_text.setText(event.getY()+"");
                double d=Math.pow((Math.pow(circle1.getCenterX()-circle2.getCenterX(),2))+
                        (Math.pow(circle1.getCenterY()-circle2.getCenterY(),2)),0.5);
                if (d<=(circle1.getRadius()+circle2.getRadius())){
                    result_label.setText("这两个圆相交么？恭喜你！相交了");
                }
                else {
                    result_label.setText("这两个圆是否相交呢？好像不相交啊");
                }
            }
        });
        circle2.setOnMouseDragged(event -> {
            if (circle2.contains(event.getX(),event.getY())){
                circle2.setCenterX(event.getX());
                circle2.setCenterY(event.getY());
                x2_text.setText(event.getX()+"");
                y2_text.setText(event.getY()+"");
                double d=Math.pow((Math.pow(circle1.getCenterX()-circle2.getCenterX(),2))+
                        (Math.pow(circle1.getCenterY()-circle2.getCenterY(),2)),0.5);
                if (d<=(circle1.getRadius()+circle2.getRadius())){
                    result_label.setText("这两个圆相交么？恭喜你！相交了");
                }
                else {
                    result_label.setText("这两个圆是否相交呢？好像不相交啊");
                }
            }
        });


        select_hbox.getChildren().addAll(pane1,pane2,draw);


        //添加总布局的组件
        pane.setTop(result_label);
        pane.setCenter(circle_pane);
        pane.setBottom(select_hbox);

        //初始化
        primaryStage.setTitle("判断圆是否相交");
        primaryStage.setScene(new Scene(pane,600,800));
        primaryStage.show();
    }
}
